<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <customer-search @search="reload"  />
    <!-- 数据表格 -->
    <el-card shadow="never">
      <ele-pro-table
        ref="table"  row-key="id" :columns="columns" :datasource="datasource" 
        :need-page="true"  @done="onDone"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          
          <el-button
            size="small"
            icon="el-icon-upload2"
            class="ele-btn-icon"
            @click="reportexcel"
          >
            导出
          </el-button>
        </template>
        <!-- 标题列 -->
        <template slot="title" slot-scope="{ row }">
          <i :class="row.icon"></i> {{ row.title }}
        </template>
          <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }"  >
          <el-link type="primary"   :underline="false"  icon="el-icon-edit"  @click="openEdit(row,'2')" >详情</el-link>
          <el-popconfirm v-if="row.isSysUser!=1" class="ele-action" title="确定要删除吗？"  @confirm="remove(row)" >
            <el-link type="danger" slot="reference"  :underline="false"  icon="el-icon-delete" >作废</el-link>
          </el-popconfirm>
      </template>
      </ele-pro-table>
    </el-card>
 
    <!-- 编辑弹窗 -->
    <customer-edit :data="current" :visible.sync="showEdit" @done="reload" :btnindex="btnindex"/>
    <!-- 导出数据 -->
    <ExportDialog ref="ExportDialog"></ExportDialog>
   
  </div>
</template>
<script>
    import CustomerEdit from './components/customer-detail';
    import CustomerSearch from './components/customer-search';
    import ExportDialog from '@/components/Excel/ExportDialog'
    import { QueryCustomerWebChartByCondition,DeleteCustomer } from '@/api/customermanager/customerwebchart';
    export default {
      name: 'User',
      components: {CustomerEdit,CustomerSearch,ExportDialog},
      data() {
        return {
          // 表格列配置
          columns: [
            {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
            {prop: 'realName', label: '客户名称', showOverflowTooltip: true, sortable: 'realName',},
            {prop: 'sexDesc', label: '性别', showOverflowTooltip: true, sortable: 'sex',},
            {prop: 'phone', label: '联系电话', showOverflowTooltip: true, align: 'left',sortable: 'phone',},
            {prop: 'cityDesc', label: '所在区域', showOverflowTooltip: true, },
            {prop: 'email', label: '邮箱', showOverflowTooltip: true, sortable: 'email',width:180},
           
            {prop: 'isBindDesc', label: '是否绑定设备', showOverflowTooltip: true,},
            {prop: 'statusDesc', label: '状态', showOverflowTooltip: true, align: 'left',sortable: 'status',},
            {prop: 'createDate', label: '入驻日期', showOverflowTooltip: true, sortable: 'createDate',width:220},
            {columnKey: 'action', label: '操作', width: 380, align: 'center', resizable: false,slot: 'action', hideInSetting: true, showOverflowTooltip: true}
          ],
          //操作按钮索引
          btnindex: '1',
          // 当前编辑数据
          current: null,
          // 是否显示编辑弹窗
          showEdit: false,
          //二维码是否展示
          ImgUrl:'',
          loading:false,
          // 导出列
          exportList: [
            { name: "客户名称", keys: "account", status: true },
            { name: "性别", keys: "realName", status: true },
            { name: "联系电话", keys: "phone", status: true },
            { name: "所在区域", keys: "cityDesc", status: true },
            { name: "邮箱", keys: "email", status: true },
            { name: '入驻日期', label: 'createDate', status: true, },
            { name: "是否绑定设备", keys: "isBindDesc", status: true },
            { name: "状态", keys: "statusDesc", status: true }
          ]
        };
      },
      methods: {
        /* 表格数据源 */
         async datasource(params) {
          params.where.pageindex = params.page;
          params.where.pagesize = params.limit;
         
          //添加排序
          if(params.order.order == null || params.order.order == undefined){
            params.where.orderBys = [{ sort: "createDate", order: 1, }];
          }else{
            params.where.orderBys = [];
            params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
          }
          let data = await QueryCustomerWebChartByCondition(params.where);

          return {
              list:data.items,
              count:data.totalrecords,
          };
        },
  
        /* 表格渲染完成回调 */
        onDone({ data }) {
          // 编辑弹窗编辑完成后 done 事件回传表单数据，使用 Object.assign 赋值给 current 即可修改表格的行数据
          if(this.current) {
              Object.assign(this.current, data);
          }
        },
        /* 刷新表格 */
        reload(where) {
          this.$refs.table.reload({ where: where });
        },
        
        /**
         * 显示编辑
         * @param row 当前行对像
         */
        openEdit(row) {
          this.current = row;
          this.showEdit = true;
          this.btnindex = index;
        },
       
        /* 删除 */
        remove(row) {
          const loading = this.$loading({ lock: true });
          DeleteCustomer(row.id)
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        },
        /* 导出 */
        reportexcel(pams) {
          this.$refs.ExportDialog.showEdits = true
          this.$refs.ExportDialog.checkList = this.exportList
          this.$refs.ExportDialog.titleName = "客户列表"
          this.$refs.ExportDialog.dataList = this.$refs.table.tableData;
        }
     
      }
    };
  </script>